{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1f242922",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['S小写:2796=一一一八五一一八五一一八五一一八四E',\n",
       " 'S字母:1134=rteyrteyrteyrteyEP',\n",
       " 'S数字:7796=31187118711871184E',\n",
       " 'S数字:5099=20398039803980396E',\n",
       " 'S小写:8386=三三五四七三五四七三五四七三五四四E',\n",
       " 'S字母:7287=woqtpoqtpoqtpoqriE',\n",
       " 'S大写:3033=壹贰壹叁叁贰壹叁叁贰壹叁叁贰壹叁贰E',\n",
       " 'S小写:1955=七八二〇七八二〇七八二〇七八二〇EP',\n",
       " 'S数字:8287=33151315131513148E',\n",
       " 'S数字:2781=11125112511251124E']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%run common.ipynb\n",
    "\n",
    "[tokenizer.decode(i) for i in tokenizer.get_batch_data(prefix=True)[1]][:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "972f2d2e",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/anaconda3/envs/pt2/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "ModelPPO(\n",
       "  (model_gen): ModelGEN(\n",
       "    (feature): GPT2Model(\n",
       "      (wte): Embedding(53, 64)\n",
       "      (wpe): Embedding(128, 64)\n",
       "      (drop): Dropout(p=0.1, inplace=False)\n",
       "      (h): ModuleList(\n",
       "        (0-3): 4 x GPT2Block(\n",
       "          (ln_1): LayerNorm((64,), eps=1e-05, elementwise_affine=True)\n",
       "          (attn): GPT2Attention(\n",
       "            (c_attn): Conv1D()\n",
       "            (c_proj): Conv1D()\n",
       "            (attn_dropout): Dropout(p=0.1, inplace=False)\n",
       "            (resid_dropout): Dropout(p=0.1, inplace=False)\n",
       "          )\n",
       "          (ln_2): LayerNorm((64,), eps=1e-05, elementwise_affine=True)\n",
       "          (mlp): GPT2MLP(\n",
       "            (c_fc): Conv1D()\n",
       "            (c_proj): Conv1D()\n",
       "            (act): NewGELUActivation()\n",
       "            (dropout): Dropout(p=0.1, inplace=False)\n",
       "          )\n",
       "        )\n",
       "      )\n",
       "      (ln_f): LayerNorm((64,), eps=1e-05, elementwise_affine=True)\n",
       "    )\n",
       "    (fc_out): Linear(in_features=64, out_features=53, bias=False)\n",
       "  )\n",
       "  (v_head): Sequential(\n",
       "    (0): Dropout(p=0.1, inplace=False)\n",
       "    (1): Linear(in_features=64, out_features=1, bias=True)\n",
       "  )\n",
       ")"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model_ppo = torch.load('ppo.model')\n",
    "model_ppo.to(device)\n",
    "model_ppo.eval()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "92cfe5fa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "S数字:6493= 25974597459745972E 25974597459745972E True\n",
      "S字母:5837= weetpeetpeetpeeriE weetpeetpeetpeeriE True\n",
      "S小写:4918= 一九六七三九六七三九六七三九六七二E 一九六七三九六七三九六七三九六七二E True\n",
      "S字母:7622= eproqproqproqpriiE eproqproqproqpriiE True\n",
      "S数字:7891= 31567156715671564E 31567156715671564E True\n",
      "S字母:6339= wtetitetitetitetyE wtetitetitetitetyE True\n",
      "S数字:4440= 17761776177617760E 17761776177617760E True\n",
      "S大写:8550= 叁肆贰零叁肆贰零叁肆贰零叁肆贰零零E 叁肆贰零叁肆贰零叁肆贰零叁肆贰零零E True\n",
      "S字母:7275= woqpwoqpwoqpwoqppE woqpwoqpwoqpwoqppE True\n",
      "S数字:4742= 18969896989698968E 18969896989698968E True\n",
      "S小写:7644= 三〇五七九〇五七九〇五七九〇五七六E 三〇五七九〇五七九〇五七九〇五七六E True\n",
      "S大写:2652= 壹零陆零玖零陆零玖零陆零玖零陆零捌E 壹零陆零玖零陆零玖零陆零玖零陆零捌E True\n",
      "S数字:7386= 29546954695469544E 29546954695469544E True\n",
      "S字母:8335= eeereeereeereeerpE eeereeereeereeerpE True\n",
      "S大写:5233= 贰零玖叁肆零玖叁肆零玖叁肆零玖叁贰E 贰零玖叁肆零玖叁肆零玖叁肆零玖叁贰E True\n",
      "S字母:5038= wpqtrpqtrpqtrpqtwE wpqtrpqtrpqtrpqtwE True\n",
      "S大写:4874= 壹玖肆玖柒玖肆玖柒玖肆玖柒玖肆玖陆E 壹玖肆玖柒玖肆玖柒玖肆玖柒玖肆玖陆E True\n",
      "S大写:1109= 肆肆叁陆肆肆叁陆肆肆叁陆肆肆叁陆EP 肆肆叁陆肆肆叁陆肆肆叁陆肆肆叁陆E False\n",
      "S数字:8917= 35671567156715668E 35671567156715668E True\n",
      "S字母:1620= yripyripyripyripEP yripyripyripyripE False\n",
      "S数字:9546= 38187818781878184E 38187818781878184E True\n",
      "S字母:1388= tttwtttwtttwtttwEP tttwtttwtttwtttwE False\n",
      "S大写:5937= 贰叁柒伍零叁柒伍零叁柒伍零叁柒肆捌E 贰叁柒伍零叁柒伍零叁柒伍零叁柒肆捌E True\n",
      "S字母:3630= qrtwqrtwqrtwqrtwpE qrtwqrtwqrtwqrtwpE True\n",
      "S大写:9355= 叁柒肆贰叁柒肆贰叁柒肆贰叁柒肆贰零E 叁柒肆贰叁柒肆贰叁柒肆贰叁柒肆贰零E True\n",
      "S数字:5381= 21526152615261524E 21526152615261524E True\n",
      "S小写:1693= 六七七二六七七二六七七二六七七二EP 六七七二六七七二六七七二六七七二E False\n",
      "S大写:7799= 叁壹壹玖玖壹壹玖玖壹壹玖玖壹壹玖陆E 叁壹壹玖玖壹壹玖玖壹壹玖玖壹壹玖陆E True\n",
      "S大写:4681= 壹捌柒贰伍捌柒贰伍捌柒贰伍捌柒贰肆E 壹捌柒贰伍捌柒贰伍捌柒贰伍捌柒贰肆E True\n",
      "S字母:8287= eeqtqeqtqeqtqeqriE eeqtqeqtqeqtqeqriE True\n",
      "S字母:9587= eietqietqietqieriE eietqietqietqieriE True\n",
      "S数字:7192= 28770877087708768E 28770877087708768E True\n",
      "S数字:1834= 7336733673367336EP 7336733673367336E False\n",
      "S小写:1540= 六一六〇六一六〇六一六〇六一六〇EP 六一六〇六一六〇六一六〇六一六〇E False\n",
      "S大写:5217= 贰零捌柒零零捌柒零零捌柒零零捌陆捌E 贰零捌柒零零捌柒零零捌柒零零捌陆捌E True\n",
      "S字母:8663= eryttryttryttrytwE eryttryttryttrytwE True\n",
      "S大写:1003= 肆零壹贰肆零壹贰肆零壹贰肆零壹贰EP 肆零壹贰肆零壹贰肆零壹贰肆零壹贰E False\n",
      "S字母:6243= wrourrourrourrouwE wrourrourrourrouwE True\n",
      "S大写:6684= 贰陆柒叁捌陆柒叁捌陆柒叁捌陆柒叁陆E 贰陆柒叁捌陆柒叁捌陆柒叁捌陆柒叁陆E True\n",
      "S小写:8930= 三五七二三五七二三五七二三五七二〇E 三五七二三五七二三五七二三五七二〇E True\n",
      "S字母:2583= qpeeepeeepeeepeewE qpeeepeeepeeepeewE True\n",
      "S大写:9191= 叁陆柒陆柒陆柒陆柒陆柒陆柒陆柒陆肆E 叁陆柒陆柒陆柒陆柒陆柒陆柒陆柒陆肆E True\n",
      "S小写:6639= 二六五五八六五五八六五五八六五五六E 二六五五八六五五八六五五八六五五六E True\n",
      "S字母:5671= wwyiywyiywyiywyirE wwyiywyiywyiywyirE True\n",
      "S字母:4443= quuueuuueuuueuuuwE quuueuuueuuueuuuwE True\n",
      "S大写:3485= 壹叁玖肆壹叁玖肆壹叁玖肆壹叁玖肆零E 壹叁玖肆壹叁玖肆壹叁玖肆壹叁玖肆零E True\n",
      "S小写:3433= 一三七三三三七三三三七三三三七三二E 一三七三三三七三三三七三三三七三二E True\n",
      "S字母:4177= qyupoyupoyupoyupiE qyupoyupoyupoyupiE True\n",
      "S字母:5731= wwowywowywowywowrE wwowywowywowywowrE True\n",
      "S字母:4038= qyqteyqteyqteyqtwE qyqteyqteyqteyqtwE True\n",
      "S数字:6853= 27414741474147412E 二7414741474147412E False\n",
      "S大写:2404= 玖陆壹陆玖陆壹陆玖陆壹陆玖陆壹陆EP 玖陆壹陆玖陆壹陆玖陆壹陆玖陆壹陆E False\n",
      "S大写:8951= 叁伍捌零柒伍捌零柒伍捌零柒伍捌零肆E 叁伍捌零柒伍捌零柒伍捌零柒伍捌零肆E True\n",
      "S数字:8145= 32583258325832580E 32583258325832580E True\n",
      "S字母:1960= uirpuirpuirpuirpEP uirpuirpuirpuirpE False\n",
      "S字母:4129= qytquytquytquytqyE qytquytquytquytqyE True\n",
      "S大写:6986= 贰柒玖肆陆柒玖肆陆柒玖肆陆柒玖肆肆E 贰柒玖肆陆柒玖肆陆柒玖肆陆柒玖肆肆E True\n",
      "S字母:2628= qptqeptqeptqeptqwE qptqeptqeptqeptqwE True\n",
      "S字母:9668= eiyutiyutiyutiyuwE eiyutiyutiyutiyuwE True\n",
      "S数字:4619= 18477847784778476E 18477847784778476E True\n",
      "S小写:9805= 三九二二三九二二三九二二三九二二〇E 三九二二三九二二三九二二三九二二〇E True\n",
      "S大写:5553= 贰贰贰壹肆贰贰壹肆贰贰壹肆贰贰壹贰E 贰贰贰壹肆贰贰壹肆贰贰壹肆贰贰壹贰E True\n",
      "S大写:9671= 叁捌陆捌柒捌陆捌柒捌陆捌柒捌陆捌肆E 叁捌陆捌柒捌陆捌柒捌陆捌柒捌陆捌肆E True\n",
      "S数字:4843= 19373937393739372E 19373937393739372E True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.84375"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#随机一批数据\n",
    "_, input_ids, _ = tokenizer.get_batch_data(prefix=True)\n",
    "\n",
    "#切分成question和answer\n",
    "split = [i.index(tokenizer.encoder['=']) + 1 for i in input_ids]\n",
    "question = [input_ids[i][:split[i]] for i in range(len(input_ids))]\n",
    "answer = [input_ids[i][split[i]:] for i in range(len(input_ids))]\n",
    "\n",
    "#根据question生成predict\n",
    "input_ids = [torch.LongTensor(i).unsqueeze(0).to(device) for i in question]\n",
    "predict = [generate(model_ppo.model_gen, i) for i in input_ids]\n",
    "\n",
    "#裁剪,只要生成的部分\n",
    "predict = [p[0].tolist()[len(q):] for p, q in zip(predict, question)]\n",
    "\n",
    "#解码成文本\n",
    "question = [tokenizer.decode(i) for i in question]\n",
    "answer = [tokenizer.decode(i) for i in answer]\n",
    "predict = [tokenizer.decode(i) for i in predict]\n",
    "\n",
    "acc = 0\n",
    "for q, a, p in zip(question, answer, predict):\n",
    "    print(q, a, p, a == p)\n",
    "    if a == p:\n",
    "        acc += 1\n",
    "\n",
    "acc / len(question)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:pt2]",
   "language": "python",
   "name": "conda-env-pt2-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
